<?php
class Bobot extends Controller{
	
	function bobot(){
		parent::Controller();
		$this->load->model('bobot_model');
		$this->load->model('analisa_model');
	}
	
	function index(){//Fungsi pertama yang dipanggil dari bobot
		if ($this->session->userdata('login') == TRUE){
			if($this->session->userdata('analisa') == TRUE){
				
				$this->analisa_model->ana_off();
				$this->get();
			}
			else {
				$this->get();
			}
		}
		else {
			redirect('home');
		}
	}
	
	var $title = 'Bobot';
	var $batas = 10;
	
	function get(){//ambil data dari database bobot
		$data['title'] = $this->title;
		$data['h2_title'] = 'Bobot';
		$data['main'] = 'bobot/bobot';
		
		// Offset
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		
		//set pagination
		$config['base_url'] = site_url('bobot/get');
		$config['total_rows'] = $this->bobot_model->count_table();
		$config['per_page'] = $this->batas;
		$config['uri_segment'] = $uri_segment;
		$this->pagination->initialize($config);
		//$data['pagination'] = $this->pagination->create_links();
		
		$tmpl = array( 'table_open'  => '<table border="0" cellpadding="0" cellspacing="0">',
					'row_alt_start'  => '<tr class="zebra">',
					'row_alt_end'    => '</tr>');
					
		$this->table->set_template($tmpl);
		
		$this->table->set_empty('&nbsp;');
		$this->table->set_heading('No','Nama','P','E','A','R','L','S','Tanggal Input','Action');
		
		$bot = $this->bobot_model->get();
		$n = 1;
		$datestring = "%d %m %Y";
		
		foreach($bot as $row){
			$this->table->add_row($n++, $row->nama_bobot, $row->p.' %', $row->e.' %', $row->a.' %', $row->r.' %', $row->l.' %', $row->s.' %',$row->waktu, anchor('bobot/update/'.$row->id_bobot,'edit',array('class' => 'update')). anchor('bobot/delete/'.$row->id_bobot,'hapus',array('class' => 'delete','onclick' => "return confirm('Anda yakin akan menghapus data bobot ini?')")));
		}
		
		$data['table'] = $this->table->generate();
		
		$data['link'] = array('link_add' => anchor('bobot/add/','Tambah Bobot',array('class' => 'add')));
		
		$this->load->view('base', $data);
	}
	
	function add(){//buka form tambah bobot
		$data['title'] = $this->title;
		$data['h2_title'] = 'Tambah Bobot Baru';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/addpros');
		
		$this->load->view('base',$data);
	}
	
	function add_gagal(){
		$data['title'] = $this->title;
		$data['h2_title'] = 'Tambah Bobot Baru';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/addpros');
		
		$data['nama_bobot'] = $this->session->userdata('nama_bobot');
		$data['n_p'] = $this->session->userdata('n_p');
		$data['n_e'] = $this->session->userdata('n_e');
		$data['n_a'] = $this->session->userdata('n_a');
		$data['n_r'] = $this->session->userdata('n_r');
		$data['n_l'] = $this->session->userdata('n_l');
		$data['n_s'] = $this->session->userdata('n_s');
		
		$this->load->view('base',$data);
	}
	
	function addpros(){//proses tambah bobot
		$data['title'] = $this->title;
		$data['h2_title'] = 'Tambah Bobot Baru';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/addpros');
		
		//validasi form
		$this->form_validation->set_rules('nama_bobot','Nama','required');
		$this->form_validation->set_rules('n_p','Nilai P','required');
		$this->form_validation->set_rules('n_e','Nilai E','required');
		$this->form_validation->set_rules('n_a','Nilai A','required');
		$this->form_validation->set_rules('n_r','Nilai R','required');
		$this->form_validation->set_rules('n_l','Nilai L','required');
		$this->form_validation->set_rules('n_s','Nilai S','required');
		
		$nama = $this->input->post('nama_bobot');
		$p = $this->input->post('n_p');
		$e = $this->input->post('n_e');
		$a = $this->input->post('n_a');
		$r = $this->input->post('n_r');
		$l = $this->input->post('n_l');
		$s = $this->input->post('n_s');
		
		$jml = $p + $e + $a + $r + $l + $s;
		
			if ($this->form_validation->run() == TRUE){
				if($jml == 100){
					$id = $this->bobot_model->get_max_id();
			
					$isi = array('id_bobot' => $id,
								'nama_bobot' => $nama,
								'p' => $p,
								'e' => $e,
								'a' => $a,
								'r' => $r,
								'l' => $l,
								's' => $s,
								'waktu' => date("Y-m-d H:i:s"));
					$this->bobot_model->simpan($isi);
					$this->session->set_flashdata('message', 'Satu data bobot berhasil disimpan!');
					redirect('bobot');
					}
				else{
					$this->session->set_flashdata('message', 'jumlah bobot yang anda masukkan tidak 100, silahkan cek ulang!');
					$sal = array('nama_bobot' => $nama,
								'n_p' => $p,
								'n_e' => $e,
								'n_a' => $a,
								'n_r' => $r,
								'n_l' => $l,
								'n_s' => $s);
					$this->session->set_userdata($sal);
					redirect('bobot/add_gagal');
					//$data['nama_bobot'] = $nama;
					//$data['n_p'] = $p;
					//$data['n_e'] = $e;
					//$data['n_a'] = $a;
					//$data['n_r'] = $r;
					//$data['n_l'] = $l;
					//$data['n_s'] = $s;
					//$this->load->view('base',$data);
				}
			}
			else{
				$this->load->view('base',$data);
			}
	}
	
	function update($id){
		$data['title'] = $this->title;
		$data['h2_title'] = 'Ubah Bobot';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/updpros');
		
		//ambil data bobot
		$bobot = $this->bobot_model->get_bobot_by_id($id);
		$this->session->set_userdata('id_bobot',$bobot->id_bobot);
		
		//set Data bobot
		$data['nama_bobot'] = $bobot->nama_bobot;
		$data['n_p'] = $bobot->p;
		$data['n_e'] = $bobot->e;
		$data['n_a'] = $bobot->a;
		$data['n_r'] = $bobot->r;
		$data['n_l'] = $bobot->l;
		$data['n_s'] = $bobot->s;
		
		$this->load->view('base',$data);
		
	}
	
	function update_gagal($id){
		$data['title'] = $this->title;
		$data['h2_title'] = 'Ubah Bobot';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/updpros');
		
		$bobot = $this->bobot_model->get_bobot_by_id($id);
		$this->session->set_userdata('id_bobot',$bobot->id_bobot);
		
		$data['nama_bobot'] = $bobot->nama_bobot;
		$data['n_p'] = $bobot->p;
		$data['n_e'] = $bobot->e;
		$data['n_a'] = $bobot->a;
		$data['n_r'] = $bobot->r;
		$data['n_l'] = $bobot->l;
		$data['n_s'] = $bobot->s;
		
		$this->load->view('base',$data);
	}
	
	function updpros(){
		$data['title'] = $this->title;
		$data['h2_title'] = 'Ubah Bobot';
		$data['main'] = 'bobot/bobot_form';
		$data['link'] = array('link_back' => anchor('bobot','kembali',array('class' => 'back')));
		$data['action'] = site_url('bobot/updpros');
		
		//validasi form
		$this->form_validation->set_rules('nama_bobot','Nama','required');
		$this->form_validation->set_rules('n_p','Nilai P','required');
		$this->form_validation->set_rules('n_e','Nilai E','required');
		$this->form_validation->set_rules('n_a','Nilai A','required');
		$this->form_validation->set_rules('n_r','Nilai R','required');
		$this->form_validation->set_rules('n_l','Nilai L','required');
		$this->form_validation->set_rules('n_s','Nilai S','required');
		
		$p = $this->input->post('n_p');
		$e = $this->input->post('n_e');
		$a = $this->input->post('n_a');
		$r = $this->input->post('n_r');
		$l = $this->input->post('n_l');
		$s = $this->input->post('n_s');
		$jml = $p + $e + $a + $r + $l + $s;
		
		if($jml == 100){
			if ($this->form_validation->run() == TRUE){
			$id = $this->session->userdata('id_bobot');
			$nama = $this->input->post('nama_bobot');
			$p = $this->input->post('n_p');
			$e = $this->input->post('n_e');
			$a = $this->input->post('n_a');
			$r = $this->input->post('n_r');
			$l = $this->input->post('n_l');
			$s = $this->input->post('n_s');
			
			$isi = array('id_bobot' => $id,
						'nama_bobot' => $nama,
						'p' => $p,
						'e' => $e,
						'a' => $a,
						'r' => $r,
						'l' => $l,
						's' => $s,
						'waktu' => date("Y-m-d H:i:s"));
			$this->bobot_model->upd($this->session->userdata('id_bobot'),$isi);
			$this->session->set_flashdata('message', 'Satu data bobot berhasil di edit!');
			redirect('bobot');
			}
		}
		else{
			$this->session->set_flashdata('message', 'jumlah bobot yang anda masukkan tidak 100, silahkan cek ulang!');
			redirect('bobot/update');
		}
	}
	
	function delete($id){
		$this->bobot_model->del($id);
		$this->session->set_flashdata('message', '1 data user berhasil dihapus');
		
		redirect('bobot');
	}
}
?>